package com.lln.cjcx.interceptor;
import com.lln.cjcx.pojo.Message;
import com.lln.cjcx.pojo.MyOkHttpClient;
import com.lln.cjcx.value.CommonValue;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.web.servlet.ModelAndView;
import yfdc.JavaBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author LiLinnan
 * @version 1.0
 * @date 2020/1/22 20:27
 */
public final class ClientInterceptor extends M {
	public ClientInterceptor() {
		super();
	}
	@Override public final String toString() {
		return "【客户端拦截器ClientInterceptor】@"+Integer.toHexString(hashCode());
	}
	@Override public final boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @Nullable Object handler) {
		try {
			super.preHandle(request, response, handler);
		} catch (Throwable ex) {
			//ignored
		}
		//System.out.println(this.toString() + this.preHandle_str());
		Intrinsics.checkNotNull(request, "request");
		Intrinsics.checkNotNull(response, "response");
		HttpSession session = request.getSession();
		if (session == null) throw new NullPointerException();
		if (session.getAttribute(CommonValue.CLIENT_NAME_IN_SESSION) == null) {
			session.setAttribute(CommonValue.CLIENT_NAME_IN_SESSION, new MyOkHttpClient());
		}
		return true;
	}

	@Override public final void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
		try {
			super.postHandle(request, response, handler, modelAndView);
		} catch (Throwable ex) {
			//ignored
		}
		//System.out.println(this.toString() + this.postHandle_str());
	}

	@Override public final void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
		try {
			super.afterCompletion(request, response, handler, ex);
		} catch (Throwable ex2) {
			//ignored
		}
		if(ex!=null){
			try{
				Intrinsics.checkParameterIsNotNull(request,"request");
				Intrinsics.checkParameterIsNotNull(response,"response");
				java.io.Writer writer=response.getWriter();
				response.setContentType("application/json;charset=utf-8");
				response.setCharacterEncoding("utf-8");
				writer.write(yfdc.GsonFactory.getGson().toJson(new Message(-2, JavaBean.stackTrace(ex))));
				response.setStatus(200);
				response.sendRedirect("about:blank");
			}catch (Throwable var100){
				//ignored
			}
		}
		//System.out.println(this.toString() + this.afterCompletion_str());
	}
}
